package com.corpgovernment.common.utils.DataPermissions;

import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.ResourceInfo;
import com.corpgovernment.common.dto.RoleResourceDTO;
import com.corpgovernment.common.handler.ICacheHandler;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.core.context.UserInfoContext;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Ks.Xing
 * <p>
 * description: 数据权限工具类
 * create: 2023-05-30 11:31
 */
@Component
public class DataPermissionsConfig {
    //根据角色ID获取资源数据
    public static final String ROLE_KEY = "ROLE_RESOURCE_%s_ROLEID";
    @Resource
    private ICacheHandler iCacheHandler;

    public RoleResourceDTO getRoleDataPermissions() {
        BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
        if (baseUserInfo.getDataPermissionControl() && Objects.nonNull(baseUserInfo.getRoleId())) {
            RoleResourceDTO roleResourceDTO = new RoleResourceDTO();
            ResourceInfo resourceInfo = getResource(baseUserInfo.getRoleId().toString(), ResourceInfo.class);
            //数据权限只有部门则 只展示部门数据      如数据权限包含公司  则只展示公司数据
            List<ResourceInfo.DeptInfo> resourceData = Optional.ofNullable(resourceInfo.getDeptInfos()).orElse(new ArrayList<>());
            List<ResourceInfo.DeptInfo> corpInfo = resourceData.stream().filter(item -> Objects.equals(item.getOrgType(), "C")).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(corpInfo)) {
                roleResourceDTO.setScopeOfAuthority(corpInfo.stream().findAny().get().getOrgType());
                roleResourceDTO.setOrgIds(corpInfo.stream().map(ResourceInfo.DeptInfo::getOrgId).collect(Collectors.toList()));
            } else {
                roleResourceDTO.setScopeOfAuthority(resourceData.stream().findAny().get().getOrgType());
                roleResourceDTO.setOrgIds(resourceData.stream().map(ResourceInfo.DeptInfo::getOrgId).collect(Collectors.toList()));
            }
            return roleResourceDTO;
        }
        return new RoleResourceDTO();
    }

    public ResourceInfo getRoleAllDataPermissions(){
        BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
        if (baseUserInfo.getDataPermissionControl() && Objects.nonNull(baseUserInfo.getRoleId())) {
            return getResource(baseUserInfo.getRoleId().toString(), ResourceInfo.class);
        }
        return null;
    }


    /**
     * 获取角色资源缓存
     */
    public <T> T getResource(String key, Class<T> tClass) {
        try {
            Object val = iCacheHandler.getValue(getKey(key));
            if (val == null){
                return null;
            }
            return JsonUtils.convert(val, tClass);
        } catch (Exception ex) {
            return null;
        }
    }

    public String getKey(String key){
        return String.format(ROLE_KEY, key);
    }

}
